package it.eng.get.pdfwe.jr;

import java.awt.Dimension;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRConstants;
import net.sf.jasperreports.engine.JRElement;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRImageRenderer;
import net.sf.jasperreports.engine.JRPrintImage;
import net.sf.jasperreports.engine.JRRenderable;
import net.sf.jasperreports.engine.JRWrappingSvgRenderer;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.util.JRTypeSniffer;

// Instead of net.sf.jasperreports.j2ee.servlets.ImageServlet
/**
 * @author <div style="width: 100%; height: 2em;
 * background-repeat: repeat-x; 
 * background-image: url(http://www.mysoftworks.net/images/author_back.gif);" >
 * <a style="float: right; font-size: .9em; margin: 1.2em .5em .1em .1em; color: #658A16; color: 658A16; font-family: helvetica; font-weight: bold; text-decoration: none" href="http://www.mysoftworks.net">
 * Antonio Giovannetti for Engineering</a>
 * </div>
 */
public class JasperImageServlet extends HttpServlet
{
	private static final long serialVersionUID = JRConstants.SERIAL_VERSION_UID;

	/**
	 *
	 */
	public static final String IMAGE_NAME_REQUEST_PARAMETER = "image";

	/**
	 *
	 */
	public void service(HttpServletRequest request, HttpServletResponse response ) throws IOException, ServletException
	{
		Map map = request.getParameterMap();
		
		byte[] imageData = null;
		String imageMimeType = null;
		String imageName = request.getParameter(IMAGE_NAME_REQUEST_PARAMETER);
		if ("px".equals(imageName))
		{
			try
			{
				JRRenderable pxRenderer = JRImageRenderer.getInstance("net/sf/jasperreports/engine/images/pixel.GIF");
				imageData = pxRenderer.getImageData();
				imageMimeType = JRRenderable.MIME_TYPE_GIF;
			}
			catch (JRException e)
			{
				throw new ServletException(e);
			}
		}
		else
		{
			List<JasperPrint> jasperPrintList = JasperPrintContainer.getList();

			if (jasperPrintList == null)
			{
				throw new ServletException("No JasperPrint documents found on the HTTP session.");
			}
			JRPrintImage image = null;
			try {
				image = JRHtmlExporter.getImage(jasperPrintList, imageName);
				
			} catch (Exception e) {
				// Forza la reinizializzaione del servlet nel caso si verifichi un erore all'interno di JRHtmlExporter.getImage
				System.out.println("Errore in JasperImageServlet");
				jasperPrintList.clear();
				e.printStackTrace();
//				this.init();
				return;
			}
//			System.out.println("AnchorName:" + image.getAnchorName());
//			System.out.println("BookmarkLevel:" + image.getBookmarkLevel());
//			System.out.println("Height:" + image.getHeight());
//			System.out.println("HorizontalAlignment:" + image.getHorizontalAlignment());
//			System.out.println("HyperlinkReference:" + image.getHyperlinkReference());
//			System.out.println("HyperlinkTarget:" + image.getHyperlinkTarget());
//			System.out.println("HyperlinkTooltip:" + image.getHyperlinkTooltip());
//			System.out.println("HyperlinkType:" + image.getHyperlinkType());
//			System.out.println("Key:" + image.getKey());
//			System.out.println("LinkTarget:" + image.getLinkTarget());
//			System.out.println("LinkType:" + image.getLinkType());
//			System.out.println("Mode:" + image.getMode());
//			System.out.println("OnErrorType:" + image.getOnErrorType());
//			System.out.println("StyleNameReference:" + image.getStyleNameReference());
//			System.out.println("VerticalAlignment:" + image.getVerticalAlignment());
//			System.out.println("Width:" + image.getWidth());
//			System.out.println("X:" + image.getX());
//			System.out.println("Y:" + image.getY());
			
			JRRenderable renderer = image.getRenderer();
			if (renderer.getType() == JRRenderable.TYPE_SVG)
			{
				renderer = 
					new JRWrappingSvgRenderer(
							renderer, 
							new Dimension(image.getWidth(), image.getHeight()),
							JRElement.MODE_OPAQUE == image.getMode() ? image.getBackcolor() : null
					);
			}

			imageMimeType = JRTypeSniffer.getImageMimeType(renderer.getImageType());

			try
			{
				imageData = renderer.getImageData();
			}
			catch (JRException e)
			{
				throw new ServletException(e);
			}
		}

		if (imageData != null && imageData.length > 0)
		{
			if (imageMimeType != null) 
			{
				response.setHeader("Content-Type", imageMimeType);
			}
			response.setContentLength(imageData.length);
			ServletOutputStream ouputStream = response.getOutputStream();
			ouputStream.write(imageData, 0, imageData.length);
			ouputStream.flush();
			ouputStream.close();
		}
	}


}
